<?php
//http://www.dailiaaa.com/?ddh=447550131760925&dq=&sl=100&issj=0&xl=3&tj=fff&api=14&cf=4&dk=80&yl=1
$arrProxyListSlow = array_unique(array_filter(explode('<br />', nl2br(trim(file_get_contents(dirname(__FILE__) . '/Slow-Proxy.txt'))))));
foreach ($arrProxyListSlow as &$proxy) {
    $proxy = preg_replace('#\s+#', '', $proxy);
}unset($proxy);

shuffle($arrProxyListSlow);

$arrProxyList = array_unique(array_filter(explode('<br />', nl2br(trim(file_get_contents(dirname(__FILE__) . '/All-Proxy.txt'))))));
foreach ($arrProxyList as &$proxy) {
    $proxy = preg_replace('#\s+#', '', $proxy);
}unset($proxy);

shuffle($arrProxyList);
foreach ($arrProxyList as $k => $v) {
    if (in_array($v, $arrProxyListSlow)) {
        unset($arrProxyList[$k]);
    }
}

$GlobalCountIncr = array();
$GlobalCountIncr['errno'] = array();
$GlobalCountIncr['ok'] = array();
@unlink(dirname(__FILE__) . '/Fast-Proxy-Processing.txt');
foreach ($arrProxyList as $proxyServer) {
    $proxy = $proxyServer;
    $ch = curl_init();
    $url = 'http://imail.sinaapp.com/ip.php';
    curl_setopt($ch, CURLOPT_PROXY, $proxy);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, $ua[mt_rand(0, count($ua) - 1)]);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    $result = curl_exec($ch);
    var_dump($result);
    if ($curl_errno = curl_errno($ch)) {
        $GlobalCountIncr['errno'][$proxy] ++;
        //log::w('proxy[' . $proxy . '] fetch errno:' . $curl_errno . ',error:' . curl_error($ch));
        file_put_contents(dirname(__FILE__) . '/Slow-Proxy.txt', $proxyServer . PHP_EOL, 8);
    } else {
        $GlobalCountIncr['ok'][$proxy] ++;
        //log::n('proxy[' . $proxy . '] fetch success,cost[' . (microtime(true) - $intTime) . ']');
        //log::n('result:'.$result);
        $arrIpHttp = explode(':', $result);
        $arrIpProxy = explode(':', $proxyServer);
        if ($arrIpHttp[0] == $arrIpProxy[0]) {
            echo $proxyServer . PHP_EOL;
            file_put_contents(dirname(__FILE__) . '/Fast-Proxy-Processing.txt', $proxyServer . PHP_EOL, 8);
        }
    }
    curl_close($ch);
}

file_put_contents(dirname(__FILE__) . '/Fast-Proxy.txt', file_get_contents(dirname(__FILE__) . '/Fast-Proxy-Processing.txt'));
?>